<?php

use Biz\model\GzDesignWeekActivity\GzDesignWeekActivityModel;
use Biz\model\GzDesignWeekActivity\GzDesignWeekActivityWinnerListModel;
use Biz\service\brand\Brand;
use Doraemon\cache\PhotoCounter;
use Doraemon\cache\UserCounter;
use Doraemon\model\Photos;
use Doraemon\service\Like;
use Doraemon\service\Follow;
use Doraemon\service\User;
use Doraemon\tools\ImgUrl;

class IndexAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currentUid = $this->uid;
        //查询头部需要展示的品牌数据
        $brandInfo = Brand::getBrandInfoByIds4Goods([GzDesignWeekActivityModel::BRAND_ID])[GzDesignWeekActivityModel::BRAND_ID] ?? [];
        if ($brandInfo) {
            $brandInfo['fans'] = UserCounter::getOneByType($brandInfo['uid'], UserCounter::TYPE_FANS) ?? 0;
            $brandInfo['liver_said'] = UserCounter::getOneByType($brandInfo['uid'], UserCounter::TYPE_LIVER_SAID) ?? 0;
            $brandInfo['is_follow'] = Follow::FollowStatus(
                    $currentUid,
                    $brandInfo['uid']
                )[$brandInfo['uid']] ?? 0; //1已关注该用户 0未关注
        }

        $taskMap = GzDesignWeekActivityModel::TASK_MAP;

        //组装任务数据
        $taskList = [];
        $tasksComplete = GzDesignWeekActivityModel::getMapByUid($currentUid);
        $percent = 0;

        foreach ($taskMap as $task) {

            $task['task_status'] = 0; //1已完成 0未完成
            if (isset($tasksComplete[$task['task_id']]) && !empty($tasksComplete[$task['task_id']])) {
                $task['task_status'] = 1; //1已完成 0未完成
                $percent += 25;
            }

            $taskList[] = $task;
        }

        //查询是否已经获奖
        $is_winner = 0;
        $isWinner = GzDesignWeekActivityWinnerListModel::isWinnerByUid($currentUid);

        //活动期内，每人最多1次中奖机会
        if ($isWinner > 0) {
            $is_winner = 1;
        }

        //查询今天是否已经参与抽奖
        $is_draw_today = 0;
        $todayIsDraw = GzDesignWeekActivityWinnerListModel::toDayIsDrawByUid($currentUid);
        if ($todayIsDraw > 0) { //1表示今天已经抽过奖
            $is_draw_today = 1;
        }
        $common = [
            "percent" => $percent,
            "is_over" => (date('Ymd') >= GzDesignWeekActivityModel::END_TIME) ? 1 : 0,//活动是否结束 1结束 0未结束
            "is_winner" => $is_winner,//是否已中奖 1已中奖 0 未中奖
            "is_draw_today" => $is_draw_today,//今天是否抽过奖 1已经抽过奖 0没有抽过奖
        ];
        $noteList = self::getNoteList($currentUid);
        $this->setSuccess(compact('brandInfo', 'taskList', 'common', 'noteList'), self::API_CODE_SUCCES);
    }

    //扩展图文动态相关内容
    public static function getNoteList($currentUid)
    {
        $noteIds = GzDesignWeekActivityModel::NOTE_ID_MAP;

        //查询需要展示的被点赞的内容
        $photoCounter = PhotoCounter::getMulti($noteIds);

        $contentList = [];

        //查询内容数据
        $noteLists = Photos::getPhotoInfoTime(
            $noteIds,
            ['id(obj_id)', 'title', 'remark', 'pic_id(cover_pic_id)', 'addtime', 'uid']
        ) ?: [];
        $userList = User::getBatchBasicUserInfo(array_column($noteLists, 'uid'));
        $userListMap = array_column($userList, null, 'uid');

        $isLikedMulti = Like::isLikedMulti($currentUid, $noteIds,
            \Doraemon\model\Like::LIKE_TYPE_PHOTO);//是否点赞了

        foreach ($noteLists as $note) {
            $isLike = $isLikedMulti[$note['obj_id']] ?? 0;
            $contentList[] = [
                'note_id' => $note['obj_id'],
                'nick' => $userListMap[$note['uid']]['nick'] ?? '',
                'avatar' => $userListMap[$note['uid']]['avatar'] ?? '',
                'title' => $note['title'] ?? '',
                'remark' => $note['remark'] ?? '',
                'cover_pic_url' => ImgUrl::getUrl(
                    ImgUrl::APPTYPE_APP,
                    $note['cover_pic_id'],
                    ImgUrl::IMAGE_SIZE_ORI_NPHONE
                ),
                'like' => $photoCounter[$note['obj_id']]['like'],
                'is_like' => $isLike,
            ];
        }
        $noteListRes = [];
        if ($contentList) {

            $contentListMap = array_column($contentList, null, 'note_id');

            foreach ($noteIds as $valNoteId) {

                $noteListRes[] = $contentListMap[$valNoteId];
            }
        }

        return array_values(array_filter($noteListRes));
    }
}
